;; Tests of the non-robust row reduced echelon functions
;; Copyright (C) 2025  Rocks Mazama
;;
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <https://www.gnu.org/licenses/>.

(ns mazama.linalg.rref-test
  (:require
   [clojure.test :refer [deftest is testing]]
   [mazama.linalg.rref :as rref]))

(deftest rref-3x3
  (testing "https://www.mathworks.com/help/matlab/ref/rref.html Example 3x3"
    (let [m {:ncols 3 :data [8 1 6
                             3 5 7
                             4 9 2]}
          result (rref/rref m)]
      (is (= [0 1 2] (:pivot-column-indexes result)))
      (is (= [[1 0 0]
              [0 1 0]
              [0 0 1]]
             (:pivot-rows result)))
      (is (nil? (:zero-rows result))))))

(deftest rref-4x4
  (testing "https://www.mathworks.com/help/matlab/ref/rref.html Example 4x4"
    (let [m {:ncols 4 :data [16     2     3    13
                             5    11    10     8
                             9     7     6    12
                             4    14    15     1]}
          result (rref/rref m)]
      (is (= [0 1 2] (:pivot-column-indexes result)))
      (is (= [[1     0     0     1]
              [0     1     0     3]
              [0     0     1    -3]]
             (:pivot-rows result)))
      (is (= [[0 0 0 0]] (:zero-rows result))))))

(deftest rref-3x4-augmented
  (testing "https://www.mathworks.com/help/matlab/ref/rref.html 3x4 Augmented"
    (let [m {:ncols 4 :acols 3 :data [8     1     6     1
                                      3     5     7     1
                                      4     9     2     1]}
          result (rref/rref m)]
      (is (= [0 1 2] (:pivot-column-indexes result)))
      (is (= [[1 0 0 1/15]
              [0 1 0 1/15]
              [0 0 1 1/15]]
             (:pivot-rows result)))
      (is (nil? (:zero-rows result))))))

(deftest rref-3x4-other
  (testing "Formerly at: http://linear.ups.edu/html/section-RREF.html, Exercise RREF.C30"
    (let [m {:ncols 4 :data [ 2  1  5 10
                              1 -3 -1 -2
                              4 -2  6 12]}
          result (rref/rref m)]
      (is (= [0 1] (:pivot-column-indexes result)))
      (is (= [[1  0  2  4]
              [0  1  1  2]]
             (:pivot-rows result)))
      (is (= [[0 0 0 0]] (:zero-rows result))))))

(deftest rref-augmented
  (testing "Formerly at https://www.usna.edu/Users/math/uhan/sm286a/rref.pdf, page 3"
    (let [m {:acol 3 :ncols 4 :data [ 2  8  4  2
                                      2  5  1  5
                                      4 10 -1  1]}
          result (rref/rref m)]
      (is (= [0 1 2] (:pivot-column-indexes result)))
      (is (= [[1  0  0 11]
              [0  1  0 -4]
              [0  0  1  3]]
             (:pivot-rows result)))
      (is (nil? (:zero-rows result))))))

(deftest rref-lemma
  (testing "lem.ma First Set of Gaussian Elimination Examples"
    (let [m {:ncols 5 :acols 4 :data [1  3 1 1  2
                                      2  6 3 4  5
                                      7 21 8 9 15]}
          result (rref/rref m)]
      (is (= '(0 2) (:pivot-column-indexes result)))
      (is (= '((1 3 0 -1 1)
               (0 0 1  2 1))
             (:pivot-rows result)))
      (is (= '((0 0 0 0 0)) (:zero-rows result))))))
